package beer.zivelin.awesome.walawala.mobile.service;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class T9 {
    private static final T9 instance = new T9();

    public static T9 getInstance() {
        return instance;
    }

    /**
     * 应用名称转拼音，若存在多音字，返回列表结果多于1
     *
     * @param appName
     * @return
     */
    public Set<String> toPinyinSentence(String appName) {
        Set<String> pinyinSentenceList = new HashSet<>();
        Set<List<String>> pinyinList = PinyinAnalyzer.toPinyin(appName);

        for (List<String> pinyin : pinyinList) {
            if (pinyin != null) {
                String pinyinSentence = Joiner.on("").join(pinyin);
                if (!Strings.isNullOrEmpty(pinyinSentence)) {
                    pinyinSentenceList.add(pinyinSentence);
                }
            }
        }
        if (pinyinSentenceList.size() == 0) {
            pinyinSentenceList.add(T9Mapping.getInstance().toT9(T9Mapping.ANY_CHARACTER));
        }
        return pinyinSentenceList;
    }
    /**
     * 应用名称转拼音，不考虑多音字，考虑全拼与简拼
     *
     * @param appName
     * @return
     */
    public Set<String> toNormnalPinyinSentence(String appName) {
        Set<String> pinyinSentenceList = PinyinAnalyzer.toNormalPinyin(appName);


        if (pinyinSentenceList.size() == 0) {
            pinyinSentenceList.add(T9Mapping.getInstance().toT9(T9Mapping.ANY_CHARACTER));
        }
        return pinyinSentenceList;
    }

    /**
     * 拼音转T9键盘
     *
     * @param pinyin
     * @return
     */
    public String toT9ByPinyin(String pinyin) {
        if (Strings.isNullOrEmpty(pinyin)) {
            return T9Mapping.getInstance().toT9(T9Mapping.ANY_CHARACTER);
        } else {
            return T9Mapping.getInstance().toT9(pinyin);
        }
    }

    /**
     * 汉字/字母转T9键盘
     *
     * @param appName
     * @return
     */
    public Set<String> toT9BySentence(String appName) {
        Set<String> t9List = new HashSet<>();
        Set<String> pinyinSentenceList = toNormnalPinyinSentence(appName);
        for (String pinyinSentence : pinyinSentenceList) {
            t9List.add(toT9ByPinyin(pinyinSentence));
        }
        return t9List;
    }
}
